package com.igeek.health.controller;

import com.aliyuncs.exceptions.ClientException;
import com.igeek.health.constant.MessageConstant;
import com.igeek.health.constant.RedisMessageConstant;
import com.igeek.health.result.Result;
import com.igeek.health.utils.SMSUtils;
import com.igeek.health.utils.ValidateCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @Author fengqq
 * @Description TODO
 * @Date 2024/9/11  14:20
 */
@RestController
@RequestMapping("/app/validateCode")
@Api(tags = "发送短信的控制层")
@CrossOrigin //处理跨越问题
public class ValidateCodeController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/sendByOrder")
    @ApiOperation("提交预约时发送短信验证码")
    //提交预约时  发送短信验证码
    public Result sendByOrder(String telephone)  {
        try {
            //1.判断 验证码在一分钟内有没有重复发送 redis中是否存储的
            String code =
                    stringRedisTemplate.opsForValue().get(telephone + "_" + RedisMessageConstant.SENDTYPE_ORDER);
            if(StringUtils.hasLength(code)){
                return new Result(false, MessageConstant.SEND_VALIDATECODE_HAS);
            }

            //2.SMSUtils 提交预约时  发送短信验证码
            Integer param = ValidateCodeUtils.generateValidateCode(4);
            SMSUtils.sendMessage(SMSUtils.VALIDATE_CODE,telephone,param.toString());

            //3.在redis中存储 一分钟有效验证码   用于 是否重复发送 1888888_001   1234
            stringRedisTemplate.opsForValue().set(
                    telephone + "_" + RedisMessageConstant.SENDTYPE_ORDER,
                    param.toString(),1, TimeUnit.MINUTES);

            //4.在redis中存储 5分钟有效验证码   用于 是否能正常预约套餐 1888888_001_1234  1234
            stringRedisTemplate.opsForValue().set(
                    telephone + "_" + RedisMessageConstant.SENDTYPE_ORDER+"_"+param,
                    param.toString(),5, TimeUnit.MINUTES);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);

        } catch (ClientException e) {
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }

    @PostMapping("/sendByLogin")
    @ApiOperation("快捷登录时发送短信验证码")
    //提交预约时  发送短信验证码
    public Result sendByLogin(String telephone)  {
        try {
            //1.判断 验证码在一分钟内有没有重复发送 redis中是否存储的
            String code =
                    stringRedisTemplate.opsForValue().get(telephone + "_" + RedisMessageConstant.SENDTYPE_LOGIN);
            if(StringUtils.hasLength(code)){
                return new Result(false, MessageConstant.SEND_VALIDATECODE_HAS);
            }

            //2.SMSUtils 提交预约时  发送短信验证码
            Integer param = ValidateCodeUtils.generateValidateCode(4);
            SMSUtils.sendMessage(SMSUtils.VALIDATE_CODE,telephone,param.toString());

            //3.在redis中存储 一分钟有效验证码   用于 是否重复发送 1888888_002   1234
            stringRedisTemplate.opsForValue().set(
                    telephone + "_" + RedisMessageConstant.SENDTYPE_LOGIN,
                    param.toString(),1, TimeUnit.MINUTES);

            //4.在redis中存储 5分钟有效验证码   用于 是否能正常预约套餐 1888888_002_1234  1234
            stringRedisTemplate.opsForValue().set(
                    telephone + "_" + RedisMessageConstant.SENDTYPE_LOGIN+"_"+param,
                    param.toString(),5, TimeUnit.MINUTES);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);

        } catch (ClientException e) {
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
